<template>
  <div class="content-container">
    <!-- 搜索栏 -->
    <div ref="adminContentSearch" class="content-search-wrap">
      <el-form 
      	class="content-search"
      	inline
      	size="mini"
      >
        <el-form-item label="菜单名称">
          <el-input v-model="userId.account" placeholder="菜单名称"></el-input>
        </el-form-item>

        <el-form-item label="菜单编号">
          <el-input v-model="userId.account" placeholder="菜单编号"></el-input>
        </el-form-item>

        <el-form-item>
    		<el-button size="mini" type="primary">搜索</el-button>
    		<el-button size="mini" type="danger">清空</el-button>
  		</el-form-item>
      </el-form>
    </div>
    <!-- 工具栏 -->
    <div ref="adminContentTools" class="content-tools-wrap hidden">
      	<div class="tools-left f-l">
      		
      		<el-button-group>
			       <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleAdd">新增</el-button>
			       <el-button size="mini" type="primary" icon="el-icon-share">导出</el-button>
			   </el-button-group>

      	</div>
      	<div class="tools-right f-r">
      		
			  <el-button size="mini" type="primary" icon="el-icon-refresh" circle @click="refresh"></el-button>
			  
      	</div>
    </div>
    <!-- 内容栏--表格 -->
    <div class="content-table-wrap" v-loading="loading" loading-type="chrome-circle-turn">
    	<el-table
        border
        v-if="data"
    		size="mini"
    		:data="data"
        height="500px"
        default-expand-all
        :expand-row-keys="['menuId']"
        row-key="menuId"
        :tree-props="{children: 'children'}"
    		style="width: 100%"
    	>
    		<el-table-column
		      type="selection"
		      width="55">
		    </el-table-column>

    		<el-table-column
    			fixed
		      	type="index"
		      	:index="tableIndex">
		    </el-table-column>

    		<el-table-column
		      fixed
		      prop="name"
		      label="菜单名称"
		      width="150">
		    </el-table-column>

		    <el-table-column
		      prop="url"
		      label="路由地址"
		      width="200">
		    </el-table-column>

		    <el-table-column
		      prop="icon"
		      label="菜单图标"
		      width="200">
		    </el-table-column>

		    <el-table-column
		      prop="code"
		      label="菜单编号"
		      width="200">
		    </el-table-column>

		    <el-table-column
		      prop="sort"
		      label="菜单排序"
		      width="80">
		    </el-table-column>


		    <el-table-column
		    	fixed="right"
		      	label="操作"
		      	width="150"
		    >

		      <template slot-scope="scope">
		      	<el-button
		      		size="mini"
		      		type="primary"
		      		@click="handleEdit(scope.$index, scope.row)"
		      	>编辑</el-button>

		      	<el-button
		      		size="mini"
		      		type="primary"
		      		@click="handleDel(scope.$index, scope.row)"
		      	>删除</el-button>

		      	
		      </template>

		    </el-table-column>

    	</el-table> 

      <!-- 分页 -->
      <div ref="adminContentPagination" class="content-pagination-wrap">
        <el-pagination
            background
            :current-page="page.currentPage"
            :page-sizes="pageSizes"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total">
        </el-pagination>
      </div> 
    </div>

    


    <!-- 修改dialog -->
    <menu-edit-dialog
    	:visible="editDialogVisible"
      :type="editType"
    	:form.sync='editForm'
    	@cancel="editDialogCancel"
      @confirm="editDialogConfirm"
    ></menu-edit-dialog>

    <!-- 分配角色dialog -->
    <el-dialog title="分配角色"
               :visible.sync="rolesDialog"
               width="40%">
      <el-scrollbar style="height:400px">
        <el-tree :data="rolesTree"
                 show-checkbox
                 node-key="id"
                 default-expand-all
                 @check="rolesChange"
                 :default-checked-keys="rolesKeys"
                 :props="{label:'name',value:'id'}">
        </el-tree>
      </el-scrollbar>
      <span slot="footer">
        <el-button type="primary"
                   size="small"
                   @click="handleSetRoles">确 定</el-button>
      </span>
    </el-dialog>



  </div>

</template>

<script>
import { mapGetters } from 'vuex'

import { add, getList, getMenu, remove, update, getMenuInfo } from "@/api/admin/menu"; 

import { getRolesTree } from "@/api/admin/roles";
import MenuEditDialog from '@/components/Dialog/menuEditDialog';
import adminMixins from "@/mixins/admin"
export default {
  name: 'mgr',
  mixins:[adminMixins],
  components:{
  	MenuEditDialog
  },
  computed: {
    ...mapGetters([
      'name',
      'permission',
      'userInfo'
    ])
  },
  data(){
  	return{
      //1新增，2修改
      editType:1,
      loading:true,
  		from:{},
  		//修改模态框的显示隐藏
  		editDialogVisible:false,
  		//分配角色模态框
  		rolesDialog:false,
  		rolesTree: [],
  		rolesKeys: [],
  		userId:{
  			account:''
  		},
  		data:null,
  		page: {
    	  pageSize: 10,
    	  currentPage: 1,
    	  total: 0
  	  },
  	  pageSizes:[10,20,30,40,50,100],
  	  editForm:null
  	}
  },
  mounted(){
    //设置dom尺寸
    this.setSize();
  	this.loadData(this.page)
  },
  methods:{
  	//请求table数据
  	loadData(page, params = {}){
  		  this.loading = true;
      	getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {

      		console.log("---views/admin/menu--loadData----------")
      		console.log(res)
        	this.loading = false;
        	this.data = this.reviseData(res.data.data);

      	});
      	
  	},
    reviseData(data){
      var key = 'children';
      var arr = data.slice();

      function toParse(arr){
        arr.forEach(function(item){
          if(item.childrenNodes && Array.isArray(item.childrenNodes)){
            item[key] = item.childrenNodes;
            toParse(item[key])
          }
          delete item.childrenNodes
        })

        return arr;
      }

      return toParse(arr)
    },


  	//转换账号状态
  	convertStatus(status){
  		if(status == 'ENABLE'){
  			return true
  		}else if(status == 'UNENABLE'){
  			return false
  		}else{
  			return false
  		}
  	},
  	//表格序号
  	tableIndex(index){
  		return index+1
  	},
    //新增菜单
    handleAdd(){
        this.editForm = null;
        this.editType = 1;
        //模态框显示
        this.editDialogVisible = true;
    },
  	//点击操作的编辑
  	handleEdit(index,row){
  		console.log("-----views/admin/menu--handleEdit------------")
  		console.log("index:"+index)
  		console.log(row)
      //row.menuId
  		//获取当前修改菜单的信息，赋值给editForm

      getMenuInfo(row.menuId).then((res)=>{
        console.log("------获取菜单-------")
        console.log(res)
        var menuInfo = res.data.data;
        this.editType = 2;
        this.editForm = menuInfo;
        //模态框显示
        this.editDialogVisible = true;
      })

      
  	},
  	//点击操作的删除
  	handleDel(index,row){
  		console.log(row)

  		this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          	confirmButtonText: '确定',
          	cancelButtonText: '取消',
          	type: 'warning'
        }).then(()=>{
        	return remove(row.menuId);
        }).then((res) => {

        	this.loadData(this.page)
          	this.$message({
            	type: 'success',
            	message: '删除成功!'
          	});
        }).catch(() => {
          	this.$message({
            	type: 'info',
            	message: '已取消删除'
          	});          
        });
  	},
  	//分配角色
  	handleRoles(index,row){
  		this.form = row;
      	this.form.roleId = this.form.roleId || '';
      	this.rolesKeys = row.roleId.split(',')
      	this.rolesDialog = true;
  	},
  	rolesChange (data, checked) {
      this.form.roleId = checked.checkedKeys.join(',')
    },
    handleSetRoles () {
      setRoles(this.form.userId, this.form.roleId).then(() => {
        this.$message.success('设置成功');
        this.rolesDialog = false;
        this.loadData(this.page);
        this.form = {}
      })
    },
  	editDialogCancel(){
  		this.editDialogVisible = false;
  		this.editForm = null;
  	},
    editDialogConfirm(form){

      if(form.type == 1){

        console.log("%c传递给接口的数据","background:#009;color:#fff")
                console.log(form.data)

        add(form.data).then((res)=>{

          this.editDialogVisible = false;
          this.loadData(this.page)
              this.$message({
                type: 'success',
                message: '添加成功!'
              });
        })

      }else if(form.type == 2){

        
        update(form.data).then((res)=>{

          this.editDialogVisible = false;
          this.loadData(this.page)
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
        })
      }
    },
    //刷新
    refresh(){
      this.loadData(this.page);
    }  
  }
}
</script>

<style lang="scss" scoped>
.content {
  &-container {

  }
  /***搜索栏***/
  &-search-wrap{
    padding: 15px;
    border-bottom: 1px solid #E8EBEE;
  }
  /***工具栏***/
  &-tools-wrap{
    padding: 15px;
  }
  /***内容栏***/
  &-table-wrap{
    position: relative;
    min-height: 350px;
    padding: 15px;
  }
  /***分页***/
  &-pagination-wrap{
    padding-top: 15px;
    text-align: right;
  }
}
</style>
